package ZZ_backup;
// OracleEmployeeDAO implementation of the 
// EmployeeDAO interface. This class can contain all
// Oracle specific code and SQL statements. 
// The client is thus shielded from knowing 
// these implementation details.


import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import LawsonTransformJava.xref.XrefDepartment;
import LawsonTransformJava.xref.XrefJobCode;
import LawsonTransformJava.xref.XrefProcessLevel;

import LawsonTransformJava.dao.DAOFactoryOracleDB;
import LawsonTransformJava.dao.DAOPRTime;
import LawsonTransformJava.dao.DAObject;
import LawsonTransformJava.data.TblPRTime;

public class PR560RecType1_backup extends DAObject implements 
    DAOPRTime, Runnable {

	private XrefJobCode xJobCodes = null;
	private XrefDepartment xDepartments = null;
	private XrefProcessLevel xProcessLevels = null;
	private int payrollYear = 0;
	
    public PR560RecType1 (File output, int payrollYear, boolean allowBlanks) throws SQLException {
    // initialization 
		try {
			this.output = new FileWriter(this.outputFile = output);
			this.payrollYear = payrollYear;
			init(allowBlanks);
		} catch (IOException e) {
			// Could no write properties file
			System.out.println("IOException: " + e);
		}
  }

  private void init(boolean allowBlanks) throws IOException, SQLException {
	    // Set the data variables
	    data = new DAOFactoryOracleDB();
	    xJobCodes = new XrefJobCode(allowBlanks);
	    xDepartments = new XrefDepartment(allowBlanks);
	    xProcessLevels = new XrefProcessLevel();
	    
//	    sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME;
	    stmtData = data.getDataStatement();	    
	    countStmtData = data.getCountStatement();
	    //Set the Date Format for output
	    df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
  }
  
  public TblPRTime findPRTime() {
	  	TblPRTime prt = new TblPRTime();
		try {
			if (whereClause==null || whereClause.equals("")) {
				whereClause = " WHERE PAYROLL_YEAR=" + payrollYear;
			} else {
				whereClause = whereClause.trim() + " AND PAYROLL_YEAR=" + payrollYear;
			}
			sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME + " " + (whereClause==null?"":whereClause);
			rslt = data.getDataStatement().executeQuery(sqlData);
			rsmd = rslt.getMetaData();
			if (rslt.next()){
				hasMoreRecs = true;
				prt = (TblPRTime) createObject(rslt, prt);
			}
			else
				hasMoreRecs = false;
			return prt;
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
	  return null;
}

  public TblPRTime findPRTime(String employeeNumber) {
	try {
		TblPRTime prt = new TblPRTime();
		
		if (whereClause==null) {
			whereClause = " WHERE EMPLOYEE=" + employeeNumber + " AND PAYROLL_YEAR=" + payrollYear;
		} else {
			whereClause = whereClause.trim() + " AND EMPLOYEE=" + employeeNumber + " AND PAYROLL_YEAR=" + payrollYear;
		}
		sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME + " " + (whereClause==null?"":whereClause);
		rslt = data.getDataStatement().executeQuery(sqlData);
		rsmd = rslt.getMetaData();
		rslt.next();
		prt = (TblPRTime) createObject(rslt, prt);
		return prt;
	} catch (SQLException e) {
	    // Could not connect to the database
		System.out.println("SQLException: " + e);
	} catch (InvocationTargetException ite) {
		System.out.println("InvocationException: " + ite);
	} catch (IllegalAccessException iae) {
		System.out.println("IllegalAccessException: " + iae);
	}
	return new TblPRTime();
  }

  public TblPRTime nextPRTime() {
	  TblPRTime prt = new TblPRTime();
		try {
			if (rslt.next()){
				hasMoreRecs = true;
				prt = (TblPRTime) createObject(rslt, prt);
			}
			else
				hasMoreRecs = false;
			return prt;
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
	  return null;
}

 
	public boolean hasMore () {
	  return hasMoreRecs;
	}

  public int countPRTime() {
	try {
		countRecs = data.getCountStatement().executeQuery("SELECT COUNT(*) FROM " + data.getSchema() + "." + TABLE_NAME + (whereClause==null?"":whereClause));
		countRecs.next();
		return recCount=countRecs.getInt(1);
		} catch (SQLException e) {
		    // Could not connect to the database
			System.out.println("SQLException: " + e);
		}
		return 0;
  }
  
  public void run() {
	  TblPRTime prt = null;
	  
	  try {
		//@ TODO DEBUG BEGIN - remove lines
//		this.setWhereClause("WHERE EMPLOYEE=100466");
		//@ TODO DEBUG END   - remove lines
		prt = findPRTime();
		while (hasMore()) {
			output.write(writePR560Layout(prt) + "\n");
			output.flush();
			prt = nextPRTime();
		}
		output.close();
	} catch (IOException e) {
		// Could not write properties file
		// Could not create output file
		System.out.println("IOException: " + e);
	}
  }
  
	//Output the PR560 format - Record Type 1
	public String writePR560Layout(TblPRTime prt) {
		return (
				"1" + "," +
				"1" + "," +
				prt.getEmployee() + "," +
				xProcessLevels.xlate(prt.getProcess_level()) + "," +
//				prt.getProcess_level() + "," +
				// @ TODO Add xlate when defined.
//				xDepartments.xlate(prt.getDepartment()) + "," +		//Department
//				prt.getDepartment() + "," +							//Department
				"," +												//Department
				prt.getCheck_id() + "," +
				prt.getPay_sum_grp() + "," +
				"," +
				prt.getPer_end_date() + "," +			//Tr_date
				prt.getPer_end_date() + "," +			//Check_date
				prt.getHours() + "," +
				prt.getPay_units() + "," +
				prt.getUnit_measure() + "," +
				prt.getWage_amount() + "," +
				prt.getNon_earnings() + "," +
				"," +
				"," +
				"," +
//				prt.getJob_code() + "," +
				xJobCodes.xlate(prt.getJob_code()) + "," +
				prt.getWork_state() + "," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				"," +
				","
				);
	}
	
	public FileWriter getOutput() {
		return this.output;
	}
}